#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using std::cout;
using std::endl;
using std::cerr;
class sock{
public:
  static int Sock_for_Tcp(){
    int listen_sock = socket(AF_INET, SOCK_STREAM, 0);
    if(listen_sock < 0){
      cerr << "sock" << endl;
    }
    return listen_sock;
  }
  static int Sock_for_Udp(){
    int listen_sock = socket(AF_INET , SOCK_DGRAM, 0);
    if(listen_sock < 0){
      cerr << "sock" << endl;
    }
    return listen_sock;
  }
  static void Bind(int sock, uint16_t port){
    struct sockaddr_in local;
    local.sin_family = AF_INET;
    local.sin_port = htons(port);
    local.sin_addr.s_addr = INADDR_ANY;
    if(bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0){
      cerr << "bind" << endl;
    }
  }
  static void Listen(int sock){
    if(listen(sock, 5) < 0){
      cerr << "listen" << endl;
    }
  }
  static int Accept(int sock){
    struct sockaddr_in peer;
    socklen_t len = sizeof(peer);
    int fd = accept(sock, (struct sockaddr*)&peer, &len);
    if(fd >= 0){
      return fd;
    }
    return -1;
  }
  static void Connect(int sock, std::string ip, uint16_t port){
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = inet_addr(ip.c_str());
    if(connect(sock, (struct sockaddr*)&server, sizeof(server)) == 0){
      cout << "connect success!" << endl;
    }
    else{
      cout << "connect failed!" << endl;
    }
  }

};
